
module Stochastic.Discretize where

class StochasticProcess a where
        drift  :: a->Dot->Double
        diff   :: a->Dot->Double
        evolve :: Discretize b=>b->a->Dot->Double->Dot
        evolve discr p dot dw = Dot newT newX
                where   newT = ((+) (getT dot) (dDt discr))
                        newX = (getX dot) + ((drift p dot)*(dDrift discr) + (diff p dot)*(dDiff discr))
 
-- t and x pair
data Dot = Dot { getT :: Double, getX :: Double }
        deriving (Show)

type Path = [Dot]
        
data GeometricBrownian = GB { gbDrift :: Double, gbDiff :: Double }
        deriving (Show)

instance StochasticProcess GeometricBrownian where
        drift p (Dot t x) = (gbDrift p) * x
        diff  p (Dot t x) = (gbDiff p)  * x

data ItoProcess = IP { ipDrift :: Double->Double->Double, ipDiff :: Double->Double->Double }

instance StochasticProcess ItoProcess where
        drift p (Dot t x) = (ipDrift p) t x
        diff  p (Dot t x) = (ipDiff  p) t x

class Discretize a where
        dDrift :: a->Double
        dDiff  :: a->Double
        dDt    :: a->Double
        
data Euler = Euler { dt :: Double }
        deriving (Show)

instance Discretize Euler where
        dDrift p = dt p
        dDiff  p = sqrt (dt p)
        dDt    p = dt p
        
